﻿using System.Speech.Synthesis;

namespace Haro.Speech
{
    public class SpeechOutput
    {
        private static PromptBuilder m_PB;
        private static SpeechSynthesizer m_Synth;

        /// <summary>
        /// Gets or sets the volume of the output speech.
        /// </summary>
        public static int Volume
        {
            get { return m_Synth.Volume; }
            set { m_Synth.Volume = value; }
        }

        /// <summary>
        /// Gets or sets the rate of the output speech.
        /// </summary>
        public static int Rate
        {
            get { return m_Synth.Rate; }
            set { m_Synth.Rate = value; }
        }

        /// <summary>
        /// Initializes the speech output system.
        /// </summary>
        public static void InitializeSpeechOutput()
        {
            if (m_PB == null && m_Synth == null)
            {
                m_PB = new PromptBuilder();
                m_Synth = new SpeechSynthesizer();
                m_Synth.Volume = 100;
                m_Synth.Rate = -3;
            }
            else
            {
                Cleanup();
                m_PB = new PromptBuilder();
                m_Synth = new SpeechSynthesizer();
            }
        }

        /// <summary>
        /// Calling this function allows the system to speak the message sent to it.
        /// </summary>
        /// <param name="Message">String containing the message that needs to be spoken.</param>
        public static void Speak(string Message)
        {
            m_PB.ClearContent();
            m_PB.StartVoice(Globals.StartVoice);
            m_PB.AppendText(Message);
            m_PB.EndVoice();
            m_Synth.SpeakAsync(m_PB);
        }

        /// <summary>
        /// Cleans up all the resources being used by SpeechOutput.
        /// </summary>
        public static void Cleanup()
        {
            m_PB = null;
            m_Synth.Dispose();
            m_Synth = null;
        }
    }
}
